﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TSharp.Core.Authorization.Resources
{
    #region 注释
    /* ====================
     * 作者：wangyao
     * 创建时间：2013/5/22 10:38:26
     * ====================
     */
    #endregion
    /// <summary>
    /// 操作类型集合类
    /// </summary>
    public class OperationSet
    {
        /// <summary>
        /// 继承操作标志
        /// </summary>
        public static int InheritedOperation = 31;
        /// <summary>
        /// 数组长度
        /// </summary>
        public readonly int size;

        /// <summary>
        /// 操作集合
        /// </summary>
        public readonly Operation[] operations;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="size">操作数组长度</param>
        public OperationSet(int size)
        {
            this.size = size + 1;
            this.operations = new Operation[size + 1];
            this.operations[size] = new Operation(InheritedOperation, "继承", null);
        }

        /// <summary>
        /// 根据索引获取操作
        /// </summary>
        /// <param name="index">索引</param>
        public Operation GetOperation(int index)
        {
            return operations[index];
        }

        /// <summary>
        /// 根据索引给操作数组赋值
        /// </summary>
        /// <param name="index">索引</param>
        /// <param name="operation">操作</param>
        public void SetOperation(int index, Operation operation)
        {
            if (operation.id >= InheritedOperation)
                throw new ArgumentOutOfRangeException("operation", operation.id, "操作Id不能大于31.");
            operations[index] = operation;
        }

        /// <summary>
        /// 根据操作ID获取操作
        /// </summary>
        /// <param name="operationId">操作ID</param>
        public Operation FindOperation(int operationId)
        {
            for (int i = 0; i < size; i++)
            {
                Operation operation = operations[i];
                if (operation != null && operation.id == operationId)
                {
                    return operation;
                }
            }
            return null;
        }

        /// <summary>
        /// 根据操作ID获取允许覆盖的操作ID
        /// </summary>
        /// <param name="operationId">操作ID</param>
        public List<int> GetAllowCoverOperations(int operationId)
        {
            var result = new List<int>(0);
            Operation operation = FindOperation(operationId);
            if (operation == null)
            {
                return result;
            }
            for (int i = 0, c = operation.CoverOperationSize; i < c; i++)
            {
                Operation opr = FindOperation(operation.GetCoverOperation(i));
                if (opr != null)
                {
                    result.Add(opr.id);
                }
            }
            return result;
        }

        /// <summary>
        /// 根据操作ID获取禁止移除的操作ID
        /// </summary>
        /// <param name="operationId">操作ID</param>
        public List<int> GetForbidRemoveOperations(int operationId)
        {
            var result = new List<int>(0);
            Operation operation = FindOperation(operationId);
            if (operation == null)
            {
                return result;
            }
            for (int i = 0; i < size; i++)
            {
                Operation opr = operations[i];
                if (opr.IsSuperOf(operationId))
                {
                    result.Add(opr.id);
                }
            }
            return result;
        }
    }
}
